home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / adv_lighting / lightPosition.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.2 KB  |  322 lines

  1. /* Copyright 1996, Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* lightPosition.c - demonstrates how the light position is affected by the 
  18.  *        modelview matrix
  19.  *
  20.  *  Left Mouse Button        - change incidence and azimuth angles
  21.  *  Middle Mousebutton        - change the twist angle based on
  22.  *                  horizontal mouse movement
  23.  *  Right Mousebutton        - zoom in and out based on vertical
  24.  *                  mouse movement
  25.  *  <l> key            - toggle light binding
  26.  *  Escape key            - exit the program
  27.  */
  28.  
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31. #include <GL/glut.h>
  32.  
  33. #include <math.h>
  34. #include <stdio.h>
  35.  
  36. /*  Function Prototypes  */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  drawScene( GLvoid );
  40. GLvoid  reshape( GLsizei, GLsizei );
  41. GLvoid  keyboard( GLubyte, GLint, GLint );
  42. GLvoid  mouse( GLint, GLint, GLint, GLint );
  43. GLvoid  motion( GLint, GLint );
  44.  
  45. void resetView( GLvoid );
  46. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  47. void printHelp( char * );
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. /* Global Variables */
  54.  
  55. static enum        lighttypes { EYE_LIGHT, SCENE_LIGHT };
  56. static GLint        lightpos;
  57.  
  58. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  59. static GLint        action;
  60.  
  61. static GLdouble        xStart = 0.0, yStart = 0.0;
  62.  
  63. static GLfloat         fovy, near, far; 
  64. static GLfloat         distance, twistAngle, incAngle, azimAngle;
  65.  
  66. void
  67. main( int argc, char *argv[] )
  68. {
  69.     GLsizei width, height;
  70.  
  71.     glutInit( &argc, argv );
  72.  
  73.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  74.     height = glutGet( GLUT_SCREEN_HEIGHT );
  75.     glutInitWindowPosition( width / 4, height / 4 );
  76.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  77.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  78.     glutCreateWindow( argv[0] );
  79.  
  80.     initgfx();
  81.  
  82.     glutMouseFunc( mouse );
  83.     glutMotionFunc( motion );
  84.     glutKeyboardFunc( keyboard );
  85.     glutReshapeFunc( reshape );
  86.     glutDisplayFunc( drawScene ); 
  87.  
  88.     printHelp( argv[0] );
  89.  
  90.     glutMainLoop();
  91. }
  92.  
  93. void
  94. printHelp( char *progname )
  95. {
  96.     fprintf(stdout, "\n%s - demonstrate how the modelview matrix "
  97.         "affects the light position\n\n" 
  98.         "Left Mousebutton    - move eye position\n"
  99.         "Middle Mousebutton    - change twist angle\n"
  100.         "Right Mousebutton    - move up / down to zoom in / out\n"
  101.         "<l> Key        - toggle light binding\n"
  102.         "Escape Key        - exit the program\n\n",
  103.         progname);
  104.  
  105.     if ( lightpos == EYE_LIGHT ) 
  106.         printf("Light position attached to Viewpoint\n");
  107.     else if ( lightpos == SCENE_LIGHT )
  108.         printf("Light position fixed in scene\n");
  109. }
  110.  
  111. GLvoid
  112. initgfx( GLvoid )
  113. {
  114.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  115.     glEnable( GL_DEPTH_TEST );
  116.  
  117.     fovy = 60.0;    /* field of view in Y */
  118.     near = 3.0;    /* Near clipping plane location */
  119.     far  = 12.0;    /* Far clipping plane location */
  120.  
  121.     resetView();
  122.  
  123.     lightpos = EYE_LIGHT;
  124.  
  125.     /* Turn on a default light */
  126.     glEnable( GL_LIGHT0 );
  127. }
  128.  
  129. GLvoid 
  130. keyboard( GLubyte key, GLint x, GLint y )
  131. {
  132.     switch (key) {
  133.     case 'l':    /* toggle light position */
  134.         if ( lightpos == EYE_LIGHT ) {
  135.             lightpos = SCENE_LIGHT;
  136.             printf("Light position fixed in scene\n");
  137.         } else if ( lightpos == SCENE_LIGHT ) {
  138.             lightpos = EYE_LIGHT;
  139.             printf("Light position attached to viewpoint\n");
  140.         }
  141.         glutPostRedisplay();
  142.         break;
  143.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  144.         exit(0);
  145.     }
  146. }
  147.  
  148. GLvoid 
  149. mouse( GLint button, GLint state, GLint x, GLint y )
  150. {
  151.     static GLint buttons_down = 0;
  152.  
  153.     if (state == GLUT_DOWN) {
  154.         switch (button) {
  155.         case GLUT_LEFT_BUTTON:
  156.             action = MOVE_EYE;
  157.             break;
  158.         case GLUT_MIDDLE_BUTTON:
  159.             action = TWIST_EYE;
  160.             break;
  161.         case GLUT_RIGHT_BUTTON:
  162.             action = ZOOM;
  163.             break;
  164.         }
  165.  
  166.         /* Update the saved mouse position */
  167.         xStart = x;
  168.         yStart = y;
  169.     } else {
  170.         if (--buttons_down == 0) 
  171.             action = MOVE_NONE;
  172.     }
  173. }
  174.  
  175. GLvoid
  176. motion( GLint x, GLint y )
  177. {
  178.     switch (action) {
  179.     case MOVE_EYE:
  180.         /* Adjust the eye position based on the mouse position */
  181.         azimAngle += (GLdouble) (x - xStart);
  182.         incAngle -= (GLdouble) (y - yStart);
  183.         break;
  184.     case TWIST_EYE:
  185.         /* Adjust the eye twist based on the mouse position */
  186.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  187.         break;
  188.     case ZOOM:
  189.         /* Adjust the eye distance based on the mouse position */
  190.         distance -= (GLdouble) (y - yStart)/10.0;
  191.         break;
  192.     default:
  193.         printf("unknown action %d\n", action);
  194.     }
  195.     
  196.     /* Update the stored mouse position for later use */
  197.     xStart = x;
  198.     yStart = y;
  199.  
  200.     glutPostRedisplay();
  201. }
  202.  
  203. void
  204. resetView( GLvoid )
  205. {
  206.     distance = near + (far - near) / 2.0;
  207.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  208.     incAngle = 60.0;
  209.     azimAngle = 0.0;
  210.     fovy = 60.0;    /* Field of view in Y angle */
  211. }
  212.  
  213. GLvoid
  214. reshape( GLsizei width, GLsizei height )
  215. {
  216.     GLdouble    aspect;
  217.  
  218.     glViewport( 0, 0, width, height );
  219.  
  220.     aspect = (GLdouble) width / (GLdouble) height;
  221.  
  222.     glMatrixMode( GL_PROJECTION );
  223.     glLoadIdentity();
  224.     gluPerspective( fovy, aspect, near, far );
  225.     glMatrixMode( GL_MODELVIEW );
  226. }
  227.  
  228. void
  229. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  230.             GLfloat twist)
  231. {
  232.     glTranslatef( 0.0, 0.0, -distance);
  233.     glRotatef( -twist, 0.0, 0.0, 1.0);
  234.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  235.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  236. }
  237.  
  238. GLvoid
  239. drawScene( GLvoid )
  240. {
  241.     /* Define a few materials properties */
  242.     GLfloat   redAmbient[] = { 0.3, 0.1, 0.1, 1.0 };
  243.     GLfloat   redDiffuse[] = { 1.0, 0.0, 0.0, 1.0 };
  244.     GLfloat   blueAmbient[] = { 0.1, 0.1, 0.3, 1.0 };
  245.     GLfloat   blueDiffuse[] = { 0.0, 0.0, 1.0, 1.0 };
  246.     GLfloat   yellowDiffuse[] = { 1.0, 1.0, 0.0, 1.0 };
  247.     GLfloat   yellowEmission[] = { 0.6, 0.6, 0.0, 1.0 };
  248.     GLfloat   defaultEmission[] = { 0.0, 0.0, 0.0, 1.0 };
  249.     GLfloat   whiteSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
  250.     GLfloat   greenSpecular[] = { 0.0, 1.0, 0.0, 1.0 };
  251.     GLfloat   defaultSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
  252.  
  253.     /* infinite light */
  254.     GLfloat   lightPosition[] = { 0.0, 0.0, 1.0, 0.0 };
  255.  
  256.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  257.  
  258.     glPushMatrix();
  259.  
  260.         if ( lightpos == EYE_LIGHT ) {
  261.             /* By setting the light position before the viewing
  262.              * transformation, the light moves with the eye.
  263.              * (In other words, it is always in the same 
  264.              * position relative to the viewpoint.)
  265.              */
  266.             glLightfv( GL_LIGHT0, GL_POSITION, lightPosition);
  267.         }
  268.  
  269.         polarView( distance, azimAngle, incAngle, twistAngle );
  270.  
  271.         XYZaxes();
  272.  
  273.         if ( lightpos == SCENE_LIGHT ) {
  274.             /* By setting the light positions after the 
  275.              * viewing transformation, the light(s) will 
  276.              * be fixed in the scene.
  277.              */
  278.             glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
  279.         } 
  280.  
  281.         glEnable( GL_LIGHTING );
  282.  
  283.         glMaterialfv( GL_FRONT, GL_EMISSION, defaultEmission );
  284.  
  285.         /* Set properties for a shiny red material,
  286.          * with a green highlight */
  287.         glMaterialfv( GL_FRONT, GL_AMBIENT, redAmbient );
  288.         glMaterialfv( GL_FRONT, GL_DIFFUSE, redDiffuse );
  289.         glMaterialfv( GL_FRONT, GL_SPECULAR, greenSpecular );
  290.         glMaterialf( GL_FRONT, GL_SHININESS, 128.0 );
  291.         glPushMatrix();
  292.             glTranslatef( -2.0, 1.5, 0.0 );
  293.             glutSolidSphere( 0.7, 31, 31 );
  294.         glPopMatrix();
  295.  
  296.         /* Set properties for a dull blue material with
  297.          *   a small white highlight */
  298.         glMaterialfv( GL_FRONT, GL_AMBIENT, blueAmbient );
  299.         glMaterialfv( GL_FRONT, GL_DIFFUSE, blueDiffuse );
  300.         glMaterialfv( GL_FRONT, GL_SPECULAR, whiteSpecular );
  301.         glMaterialf( GL_FRONT, GL_SHININESS, 20.0 );
  302.         glPushMatrix();
  303.             glTranslatef( 2.5, 0.0, 0.0 );
  304.             glutSolidTorus( 0.25, 0.75, 16, 31 );
  305.         glPopMatrix();
  306.  
  307.         /* Set properties for a yellow glowing material */
  308.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellowDiffuse);
  309.         glMaterialfv( GL_FRONT, GL_EMISSION, yellowEmission );
  310.         glMaterialfv( GL_FRONT, GL_SPECULAR, defaultSpecular );
  311.  
  312.         glPushMatrix();
  313.             glTranslatef( 0.0, 2.0, 2.0 );
  314.             glutSolidCube( 0.5 );
  315.         glPopMatrix();
  316.  
  317.         glDisable( GL_LIGHTING );
  318.  
  319.     glPopMatrix();
  320.     glutSwapBuffers();
  321. }
  322.